/**
 * 
 */
package com.taobao.wolfwolf.hibernate.dao.impl;

import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.taobao.wolfwolf.hibernate.dao.BaseDao;

/**
 * @author Tux
 *
 * 创建时间: 2011-6-7 上午09:44:20
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {
	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.BaseDao#countByCriteria(org.hibernate.criterion.DetachedCriteria)
	 */
	public long countByCriteria(Class c) throws Exception {
		DetachedCriteria dc = DetachedCriteria.forClass(c);
		long count = 0L;
		
		dc.setProjection(Projections.rowCount());
		
		List list = this.getHibernateTemplate().findByCriteria(dc);
		if (list != null && list.size() > 0) {
			Long l = (Long) list.get(0);
			count = l.longValue();
		}
		return count;
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.BaseDao#create(java.lang.Object)
	 */
	public void create(Object entity) throws Exception {
		getHibernateTemplate().save(entity);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.BaseDao#findByCriteria(org.hibernate.criterion.DetachedCriteria)
	 */
	public Collection findByCriteria(DetachedCriteria dc) throws Exception {
		Collection result = this.getHibernateTemplate().findByCriteria(dc);   
		return result;
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.BaseDao#findByCriteriaByPage(org.hibernate.criterion.DetachedCriteria, int, int)
	 */
	public List findByCriteriaByPage(Class c, int page, int limit) throws Exception {
		DetachedCriteria dc = DetachedCriteria.forClass(c);
		
		return this.getHibernateTemplate().findByCriteria(dc, page, limit);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.BaseDao#get(java.lang.Class, int)
	 */
	public Object get(Class c, int id) throws Exception {
		return getHibernateTemplate().load(c, id);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.BaseDao#remove(java.lang.Object)
	 */
	public void remove(Object entity) throws Exception {
		getHibernateTemplate().delete(entity);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.BaseDao#update(java.lang.Object)
	 */
	public void update(Object entity) throws Exception {
		getHibernateTemplate().update(entity);
	}

	public Session getLocalSession() throws Exception {
		return super.getSession();
	}

	public void removeList(List list) throws Exception {
		getHibernateTemplate().deleteAll(list);
	}

	public List findByProperty(Class c, String name, Object value) throws Exception {
		DetachedCriteria dc = DetachedCriteria.forClass(c);
		
		dc.add(Restrictions.like(name, value));
		
		return getHibernateTemplate().findByCriteria(dc);
	}

	public List findByProperty(Class c, String name, Object value, int pattern)
			throws Exception {
		switch(pattern) {
		case 1:
			if(value instanceof String)
				value = "%" + value + "%";
			break;
		case 2:
			break;
		case 3:
			if(value instanceof String)
				value = value + "%";
			break;
		case 4:
			if(value instanceof String)
				value = "%" + value;
			break;
		}
		
		return findByProperty(c, name, value);
	}

	@Override
	public void saveOrUpdate(Object entity) throws Exception {
		getHibernateTemplate().saveOrUpdate(entity);
	}

	@Override
	public List findPagingByProperty(String hql) throws Exception {
		Query query = getSession().createQuery(hql);
		return query.list();
	}
}
